package com.yang.service.impl;

import com.yang.mapper.EmpMapper;
import com.yang.pojo.Emp;
import com.yang.pojo.PageBean;
import com.yang.service.EmpService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Resource(name = "empMapper")
    private EmpMapper empMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize,
                         String name, Integer gender,
                         LocalDate begin,
                         LocalDate end) {
        // 1.获取总记录数
        Long count = empMapper.count(name, gender, begin, end);
        // 2.获取分页查询结果列表

        Integer start = (page - 1) * pageSize;
        List<Emp> empList = empMapper.page(start, pageSize, name, gender, begin, end);

//        List<Emp> list = empMapper.list(name, gender, begin, end);
        // 3.封装PageBean对象
        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void add(Emp emp) {

        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp getById(Integer id) {
        return empMapper.getById(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUsernameAndPassword(emp);
    }
}
